package com.oms.common;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @Author zhanghailong
 * @Description TODO
 * @Date 16:47 2020/3/26
 */
@ApiModel
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ResponseDTO<T> {

    @ApiModelProperty(value="状态码")
    protected Integer code;

    @ApiModelProperty(value="错误消息")
    protected String msg;

    @ApiModelProperty(value="实际返回数据")
    protected T data;

    @ApiModelProperty(value="成功失败标识")
    protected Boolean success;

    private ResponseDTO(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    private ResponseDTO(ResponseCodeConst responseCodeConst) {
        this.code = responseCodeConst.getCode();
        this.msg = responseCodeConst.getMsg();
        this.success = responseCodeConst.isSuccess();
    }

    public ResponseDTO(int code, String msg, T data) {
        super();
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public ResponseDTO(ResponseCodeConst codeConst, String msg) {
        super();
        this.code = codeConst.getCode();
        this.msg = msg;
        this.success = codeConst.isSuccess();
    }

    public static <T> ResponseDTO<T> succ() {
        return new ResponseDTO<T>(ResponseCodeConst.SUCC);
    }

    public static <T> ResponseDTO<T> succMsg(String msg) {
        return new ResponseDTO<T>(ResponseCodeConst.SUCC, msg);
    }

    public static <T> ResponseDTO<T> succData(T t) {
        return new ResponseDTO<T>(ResponseCodeConst.SUCC.getCode(), ResponseCodeConst.SUCC.getMsg(), t,ResponseCodeConst.SUCC.isSuccess());
    }

    public static <T> ResponseDTO<T> succStr(T t) {
        return new ResponseDTO<T>(ResponseCodeConst.SUCC.getCode(), ResponseCodeConst.SUCC.getMsg(), t,ResponseCodeConst.SUCC.isSuccess());
    }

    public static <T> ResponseDTO<T> wrap(ResponseCodeConst codeConst) {
        return new ResponseDTO<>(codeConst.getCode(), codeConst.getMsg());
    }

    public static <T> ResponseDTO<T> wrapData(ResponseCodeConst codeConst, T t) {
        return new ResponseDTO<T>(codeConst.getCode(), codeConst.getMsg(), t);
    }

    public static <T> ResponseDTO<T> wrapMsg(ResponseCodeConst codeConst, String msg) {
        return new ResponseDTO<T>(codeConst.getCode(), msg);
    }

    public static <T> ResponseDTO<T> wrap(ResponseDTO<T> responseDTO) {
        return responseDTO;
    }

    @Override
    public String toString() {
        return "ResponseDTO{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                ", success=" + success +
                '}';
    }
}
